import ogre.renderer.OGRE as ogre
from EventController import Events

from CombatSystem import WeaponSystemTypes

import ogre.sound.OgreAL as OgreAL
import random

class WorldController:
    """
    A world controller
    """
    def __init__(self, eventController, cinematicController, sceneManager):
        pageDimension = [32.0, 100.0]
        numOfZLayers = 5
        self.cinematicController = cinematicController
        self.layers = [range(numOfZLayers)]
        self.particleSystemsPerPool = 10
        self.particleSystems = []
        self.currentParticleSystemId = []
        self.soundSystems = []
        self.soundManager = OgreAL.SoundManager()
        
        #Hmm wtf am I doing here?
        for i in range(WeaponSystemTypes.COUNT):
            system = []
            sounds = []
            self.currentParticleSystemId.append(0)
            for j in range(0, self.particleSystemsPerPool):
                id = i * self.particleSystemsPerPool + j
                ps = sceneManager.createParticleSystem("AttackParticle"+str(id), WeaponSystemTypes.MATERIALS[i])
                node = sceneManager.getRootSceneNode().createChildSceneNode()
                node.attachObject(ps)
                node.setVisible(False)
                system.append((ps, node))
                
            self.particleSystems.append(system)
        
        explosionSystems = []    
        #explosion particles
        for i in range(0, self.particleSystemsPerPool):
            ps = sceneManager.createParticleSystem("ExplosionParticle"+str(i), "explosionTemplate")
            ps.fastForward(1.0)
            ps.setKeepParticlesInLocalSpace(True)
            
            #ps.setSpeedFactor(2.5)
            node = sceneManager.getRootSceneNode().createChildSceneNode()
            node.attachObject(ps)
            node.setVisible(False)
            node.setScale(0.1, 0.1, 0.1)
            explosionSystems.append((ps, node))
        
        self.explosionSystems = explosionSystems
            
        soundPool = []        
        engineSound = []
        sound = self.soundManager.createSound("AttackParticleSound1", WeaponSystemTypes.SOUNDFX[0], False)
        sound.setGain(0.1)
        soundPool.append(sound)
        sound = self.soundManager.createSound("AttackParticleSound2", WeaponSystemTypes.SOUNDFX[1], False)
        soundPool.append(sound)
        sound.setGain(0.05)
        sound = self.soundManager.createSound("Engine_Hum", "sndaoa2.ogg", True)
        sound.setGain(0.8)
        sound.play()
        sound = self.soundManager.createSound("Engine_Hum2", "sndaoa1.ogg", True)
        sound.setGain(1.2)
        sound.play()
        
        
        
        
        self.soundPool = soundPool
     
            
        eventController.subscribe(Events.ONATTACK, self.onAttackEvent, "WorldController")
        
        return None
    
    def update(self, dt):
        
        return
    
    def getAttackParticleSystem(self, id):
        curId = self.currentParticleSystemId[id]
        curId = curId % self.particleSystemsPerPool
        self.currentParticleSystemId[id] = curId + 1
        return curId
    
    def getPageContext(self):
        """
        Return the current page context. A page is divided into Z layers. 
        """
        pageCtx = [ogre.Vector3(0.0, 0.0, self.cinematicController.cameraNode.getPosition().z)
                   ,self.layers] #return some bullshit context for testing.
        return pageCtx
    
    def onAttackEvent(self, params):
        #Play Particles    
        id = params[0]
        emitPosition = params[1]
        targetPosition = params[2]
        isHenchmen = params[3]
        currentId = self.getAttackParticleSystem(id)
        ps = self.particleSystems[id][currentId]
        ps[0].getEmitter(0).setEnabled(True)
        ps[1].setVisible(True)
        ps[1].setPosition(emitPosition)
        direction = (targetPosition - emitPosition).normalisedCopy()
        orient = ogre.Quaternion()
        xAxis = ogre.Vector3(0.0, 1.0, 0.0).crossProduct(direction)
        orient.FromAxes(xAxis, ogre.Vector3(0.0, 1.0, 0.0), direction)
        
        ps[1].setOrientation(orient)
        
        #explosion
        if isHenchmen: #only display explosion effects for henchmen firing
            ps = self.explosionSystems[currentId]
            ps[0].getEmitter(0).setEnabled(True)
            #ps[0].fastForward(1.0)
            ps[1].setPosition(targetPosition)
            ps[1].setVisible(True)
        
        
        #Play Sound
        
        sound = self.soundPool[id]
        sound.setPosition(ps[1].getPosition())
        sound.setVelocity(0, 0, 100)
        sound.setDirection(direction)
        sound.play()
                
        """
        emitter = ps[0].getEmitter(0)
        affector = ps[0].getAffector(0)
        directionEmit = ogre.Vector3(direction.x, direction.y, direction.z)
        direction *= 10.0
        #affector.setParameter("force_vector", str(direction.x) + str(direction.y) + str(direction.z))
        emitter.setDirection(directionEmit)
        """
    
    
    
